clear
clear matrix
set more off

* set main path
global path ".../data_package_fm/data"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////// 1. PREPARE DATASET ///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	* load master dataset
	use			"$path/master.dta", clear
		
	*******************************************************************************************
	*******************************************************************************************
	*** Master dataset defined at the event (brokerhouse closure or merger) x loan level.	***
	***																						***
	*** "eventid": unique ID for each event (merger or closure).							***
	*** "facilityid": unique ID for each loan tranche. 										***
	***																						***
	*** The combination of eventid + facilityid uniquely identify an observation.			***
	***																						***
	*** Sample restriction: only loans issued in the +/- 2 year window around the event		***
	***						only firms with analyst coverage in pre merger/closure year.	***
	***						see paper for further details.									***
	***																						***
	*** Description other variables: see variable labels.									***
	***																						***
	*******************************************************************************************
	*******************************************************************************************
	
	* basic diff-in-diff vars
	gen 		post			= (dealactivedate > eventdate)
	gen			post_treat		= post*treat
		
	* flag firms that are active in the pre and post period of a merger
	bys			eventid gvkey: egen meanpost = mean(post) 
	gen			act_pre_post = meanpost > 0 & meanpost < 1
	drop		meanpost
	
	* other vars
	gen			ln_at 		= ln(at)
	gen 		ln_numcov 	= ln(1+numcov)
	gen 		ln_numsweep = ln(1+numsweep)
	gen			sweep_ratio	= numsweep/(numsweep+numcov)
	gen			year		= year(dealactivedate)
	replace 	facsize 	= facsize/1000000 // in million

	* merger x firm fe
	bys			eventid gvkey: gen event_firm_fe = _n == 1
	replace		event_firm_fe = sum(event_firm_fe)
	
	* merger x post fe
	bys			eventid post: gen event_post_fe = _n == 1
	replace		event_post_fe = sum(event_post_fe)
	
	*****************************
	*** Define matched sample ***
	*****************************
	
	* match treat and control firms (for each merger event)
	local		i = 1
	levelsof	eventid, local(levels) 
	foreach		l of local levels {
		
		preserve
			keep		if eventid				== `l'
			keep		if act_pre_post			== 1
			keep		if post					== 0
			keep		eventid gvkey treat ln_at
			capture		duplicates	drop
			capture		collapse	(max) ln_at, by(eventid gvkey treat)
			isid		eventid gvkey
			sort		eventid gvkey
			capture		psmatch2	treat ln_at, noreplacement caliper(0.1)
			capture		keep		if _weight == 1
			keep		eventid gvkey
			capture		duplicates	drop
			if			`i' == 1 tempfile	matched
			if			`i' >  1 append using `matched'
			save		`matched', replace
			local		++i
		restore
		
		}

	merge	m:1 eventid gvkey using `matched', assert(1 3)
	gen		matched = _merge == 3 // flag observations in matched sample
	drop	_merge

	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////// 2. REGRESSIONS ///////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

**************************************************************
* Table 1. Basic descriptives - treat vs. control firms 
**************************************************************
	
	* set variables for descriptive statistics
	global 	firm_descr		"at lev mtb prof tang cov curr"
	global 	loan_descr		"facsize facmat d_sweep numsweep d_fincov numcov n_perfcov n_capcov"
	
	tabstat	$loan_descr $firm_descr if treat == 1 & matched == 1, stats(N mean p25 p50 p75 sd) c(s) format(%9.2fc)
	tabstat	$loan_descr $firm_descr if treat == 0 & matched == 1, stats(N mean p25 p50 p75 sd) c(s) format(%9.2fc)
	
	* ttests
	foreach var of varlist $loan_descr $firm_descr {
		ttest `var' if matched == 1, by(treat)
	}
	
********************************************************************************
* Table 2a): Basic first stage test at the firm level (Column 1 in Table 2)
********************************************************************************

preserve
	
	* load basic dataset for all DealScan firms that are in the I/B/E/S dataset
	*
	* (dataset contains for each firm and event the number of analysts covering the firm
	*  over the [-2,+2] period around the event)	
	use			"$path/coverage.dta", clear
	
	isid		eventid gvkey
	
	* change in coverage t+2 vs t-1
	gen			d_cov = analysts_plu_2 - analysts_min_1
	winsor2		d_cov, replace cuts(1 99)
	
	* temp save for use below
	isid		eventid gvkey
	tempfile	d_cov
	save		`d_cov', replace
	
	* flag treat firms
	* ("treatment_sample.dta" contains a list of treat gvkeys for each event)
	merge 		m:1 gvkey eventid eventdate using "$path/treat_sample.dta"
	gen			treat = _merge == 3
	drop		_merge
	
	* flag matched sample
	merge		m:1 eventid gvkey using `matched', assert(1 3)
	gen			matched = _merge == 3
	drop		_merge
	
	* change in coverage at the firm level
	qui: eststo m1: reg d_cov treat if matched == 1, vce(cluster gvkey)
	esttab m1, star(* 0.10 ** 0.05 *** 0.01) b(%9.3f) p(%9.3f) o(treat) keep(treat) nogaps stats(N r2_a)

restore


**************************************************************************
* Table 2b): Borkerhouse merger effect on sweep and financial covenants
**************************************************************************

* define controls
global loanFE	"facpurpose factype"
global firm		"ln_at lev mtb prof tang cov curr"
global loan		"ln_facsize ln_facmat"

* number analysts pre and post merger
merge		m:1 eventid gvkey using `d_cov', nogen keepusing(analysts_*) keep(1 3)
gen			period = -2 if (eventdate - dealactivedate) >= 0    & (eventdate - dealactivedate) <   360
replace		period = -1 if (eventdate - dealactivedate) >= 360  & (eventdate - dealactivedate) <=  720
replace		period = 1  if (eventdate - dealactivedate) <  0    & (eventdate - dealactivedate) >  -360
replace		period = 2  if (eventdate - dealactivedate) <= -360 & (eventdate - dealactivedate) >= -720
gen			a_cov =  analysts_min_2 if period == -2
replace		a_cov =  analysts_min_1 if period == -1	
replace		a_cov =  analysts_plu_1 if period == 1	
replace		a_cov =  analysts_plu_2 if period == 2
drop		period

* change in coverage at the loan level around events (Table 2, column 2)
qui: 		eststo m1: reghdfe a_cov 		post_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)		
			
* change in sweep covenants around events (Table 2, columns 3-5)
qui: 		eststo m2: reghdfe d_sweep 		post_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
			
qui: 		eststo m3: reghdfe ln_numsweep 	post_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
			
qui: 		eststo m4: reghdfe sweep_ratio 	post_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
				
esttab 		m1 m2 m3 m4, star(* 0.10 ** 0.05 *** 0.01) b(%9.3f) p(%9.3f) ///
			o(post_treat) keep(post_treat) nogaps stats(N r2_a)

* change in financial covenants around events (Table 2, columns 6-8)	
qui: 		eststo m2: reghdfe d_fincov 	post_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
			
qui: 		eststo m3: reghdfe ln_numcov 	post_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
			
qui: 		eststo m4: reghdfe pct_perfcov 	post_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
				
esttab 		m2 m3 m4, star(* 0.10 ** 0.05 *** 0.01) b(%9.3f) p(%9.3f) ///
			o(post_treat) keep(post_treat) nogaps stats(N r2_a)

			
**************************************************************************
* Table 3: Dynamic effects
**************************************************************************
	
* define time to event
gen 		dif				= (dealactivedate - eventdate)
gen			pre_2			= inrange(dif,-720,-361) // -2 years
gen			pre_1			= inrange(dif,-360,0) // -1 year
gen			post_1			= inrange(dif,1,360) // +1 year
gen			post_2			= inrange(dif,361,720) // +2 years
assert		pre_2 + pre_1 + post_1 + post_2 == 1
	
* event time x treatment
gen			pre_2_treat	 = pre_2*treat
gen			pre_1_treat	 = pre_1*treat
gen			post_1_treat = post_1*treat	
gen			post_2_treat = post_2*treat
	
qui: 		eststo m1: reghdfe d_sweep 		pre_2_treat post_1_treat post_2_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
			
qui: 		eststo m2: reghdfe ln_numsweep 	pre_2_treat post_1_treat post_2_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
			
qui: 		eststo m3: reghdfe sweep_ratio 	pre_2_treat post_1_treat post_2_treat $firm $loan	if matched == 1, /// 
			absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
	
esttab 		m1 m2 m3, star(* 0.10 ** 0.05 *** 0.01) b(%9.3f) p(%9.3f) ///
			o(pre_2_treat pre_1_treat  post_1_treat post_2_treat) ///
			keep(pre_2_treat pre_1_treat  post_1_treat post_2_treat) ///
			nogaps stats(N r2_a)

			
**********************************************************************
* Table 4: Effect by initial coverage
**********************************************************************
	
* diff-in-diff for different levels of pre-merger analyst coverage
gen			post_treat_vlow = (post_treat == 1 & inrange(analysts_min_1,0,10)  & analysts_min_1 != .) // 1  - 10 analysts
gen			post_treat_low  = (post_treat == 1 & inrange(analysts_min_1,11,20) & analysts_min_1 != .) // 11 - 20 analysts
gen			post_treat_med  = (post_treat == 1 & inrange(analysts_min_1,21,30) & analysts_min_1 != .) // 21 - 30 analysts
gen			post_treat_hig  = (post_treat == 1 & 		 analysts_min_1 >  30  & analysts_min_1 != .) //    > 30 analysts

	
local k = 1

foreach var of varlist d_sweep ln_numsweep sweep_ratio d_fincov ln_numcov pct_perfcov {

qui: 		eststo m`k': reghdfe `var' post_*low post_treat_med post_treat_hig $firm $loan if matched == 1, ///
			absorb(event_firm_fe event_post_fe rating $loanFE) vce(cluster gvkey)
			
test 		post_treat_vlow = post_treat_hig
lincom 		post_treat_vlow - post_treat_hig
local 		++k

}

esttab 		m1 m2 m3 m4 m5 m6, star(* 0.10 ** 0.05 *** 0.01) b(%9.3f) p(%9.3f) keep(post_treat*) nogaps stats(N r2_a)


**********************************************************************
* Table 5: Effect by corporate governance
**********************************************************************

* define sample splits as firm fe (by merger)
local		i = 1
levelsof	eventid, local(levels) 
foreach		l of local levels {
		   
	preserve
		keep		if eventid	== `l'
		keep		if post		== 0
		gen 		unrated 	= rating == 22 // 22 = unrated
			
		* restrict to relevant variables
		keep		gvkey eventid cashrat unrated longten cashcomp numinst
		capture		duplicates	drop
		capture		collapse	(mean) cashrat unrated longten cashcomp numinst, by(gvkey eventid)
			
		* define indicator variables
		replace		unrated 	= 0 if unrated != 1 // very few firms that obtained rating in pre period
		gen			rated 		= abs(unrated-1)
		replace		longten		= 1 if longten	!= . & longten	> 0
		gen			shortten	= abs(longten-1)
			
		* else, median splits
		foreach		var of varlist cashrat cashcomp numinst {	
			capture	xtile		x`var'	= `var', n(2)
			capture	gen			`var'_hih	= 	x`var' == 2
			capture	gen			`var'_low	=	x`var' == 1
			}
			
		capture		keep		*_high *_low *rated longten shortten gvkey eventid
		if			`i' == 1 tempfile	samplesplit
		if			`i' >  1 append using `samplesplit'
		save		`samplesplit', replace
		local		++i
	restore
	}

merge	m:1 eventid gvkey using `samplesplit', assert(1 3) nogen
	
* post x treatment x split
foreach var of varlist unrated - numinst_low longten {
	gen pt_`var' = `var'* post_treat
	}
	
qui: eststo m1:	reghdfe d_sweep pt_unrated 			pt_rated 			$firm $loan	 if matched == 1, ///
				absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
lincom 			pt_unrated - pt_rated

qui: eststo m2:	reghdfe d_sweep pt_cashrat_hih 		pt_cashrat_low		$firm $loan	 if matched == 1, ///
				absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
lincom 			pt_cashrat_hih - pt_cashrat_low

qui: eststo m3:	reghdfe d_sweep pt_longten 			pt_shortten 		$firm $loan	 if matched == 1, ///
				absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
lincom 			pt_longten - pt_shortten

qui: eststo m4:	reghdfe d_sweep pt_cashcomp_hih 	pt_cashcomp_low 	$firm $loan	 if matched == 1, ///
				absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
lincom 			pt_cashcomp_hih - pt_cashcomp_low

qui: eststo m5:	reghdfe d_sweep pt_numinst_hih 		pt_numinst_low  	$firm $loan	 if matched == 1, ///
				absorb(event_post_fe event_firm_fe rating $loanFE) vce(cluster gvkey)
lincom 			pt_numinst_hih - pt_numinst_low 

esttab m1 m2 m3 m4 m5, star(* 0.10 ** 0.05 *** 0.01) b(%9.3f) p(%9.3f) keep(pt*) nogaps stats(N r2_a)


**********************************************************************
* Table 6: Dualholders
**********************************************************************

* Panel A	
qui:	eststo m1:	reghdfe d_sweep i.post##i.treat##i.d_dual	$firm $loan	if matched == 1, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)

qui:	eststo m2:	reghdfe ln_numsweep i.post##i.treat##i.d_dual	$firm $loan	if matched == 1, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)

qui:	eststo m3:	reghdfe sweep_ratio i.post##i.treat##i.d_dual	$firm $loan	if matched == 1, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)

		
esttab m1 m2 m3, star(* 0.10 ** 0.05 *** 0.01) b(%9.3f) p(%9.3f) keep(1.post#1.treat 1.post#1.treat#1.d_dual) nogaps stats(N r2_a)
						
* Panel B
qui:	eststo m1:	reghdfe d_sweep	post_treat	$firm $loan	if matched == 1 & d_dual==0, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)

qui:	eststo m2:	reghdfe ln_numsweep	post_treat	$firm $loan	if matched == 1 & d_dual==0, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)
				
qui:	eststo m3:	reghdfe sweep_ratio	post_treat	$firm $loan	if matched == 1 & d_dual==0, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)
				
qui:	eststo m4:	reghdfe d_sweep	post_treat	$firm $loan	if matched == 1 & d_dual==1, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)
				
qui:	eststo m5:	reghdfe ln_numsweep	post_treat	$firm $loan	if matched == 1 & d_dual==1, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)

qui:	eststo m6:	reghdfe sweep_ratio	post_treat	$firm $loan	if matched == 1 & d_dual==1, /// 
		absorb(event_post_fe event_firm_fe rating $loanFE ) vce(cluster gvkey)
	
		
esttab m1 m2 m3 m4 m5 m6, star(* 0.10 ** 0.05 *** 0.01) b(%9.3f) p(%9.3f) keep(post_treat) nogaps stats(N r2_a)

* eof
